﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace ClipboardTranslator
{
    public partial class ResultForm : Form
    {
        MainForm main_form = null;

        string url = null;

        bool link_clicked = false;

        bool restoring_bounds = false;

        bool should_save = false;

        public ResultForm(MainForm main_form, bool default_location)
        {
            InitializeComponent();
            this.main_form = main_form;
            main_form.Settings.PropertyChanged += new PropertyChangedEventHandler(settings_PropertyChanged);
            if (default_location || 0 == main_form.Settings.FormWidth || 0 == main_form.Settings.FormHeight) StartPosition = FormStartPosition.WindowsDefaultLocation;
            else
            {
                StartPosition = FormStartPosition.Manual;
                restoring_bounds = true;
                Left = main_form.Settings.FormLeft;
                Top = main_form.Settings.FormTop;
                Width = main_form.Settings.FormWidth;
                Height = main_form.Settings.FormHeight;
                restoring_bounds = false;
            }
            TopMost = main_form.Settings.FormTopMost;
        }

        void settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ("FormTopMost" == e.PropertyName)
                TopMost = main_form.Settings.FormTopMost;
        }

        public void Navigate(string url, string text, string html)
        {
            this.url = url;
            Text = text;
            if (!string.IsNullOrEmpty(webBrowser.DocumentText))
                webBrowser.Stop();
            link_clicked = false;
            webBrowser.DocumentText = html;
            SaveBounds();
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement link in webBrowser.Document.Links)
                link.Click += new HtmlElementEventHandler(link_Click);
        }

        void link_Click(object sender, HtmlElementEventArgs e)
        {
            link_clicked = true;
        }

        private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (link_clicked)
            {
                e.Cancel = true;
                Process.Start(url);
            }
        }

        private void ResultForm_ResizeEnd(object sender, EventArgs e)
        {
            if (!restoring_bounds)
            {
                main_form.Settings.FormWidth = Width;
                main_form.Settings.FormHeight = Height;
                should_save = true;
            }
        }

        private void ResultForm_Move(object sender, EventArgs e)
        {
            if (!restoring_bounds)
            {
                main_form.Settings.FormLeft = Left;
                main_form.Settings.FormTop = Top;
                should_save = true;
            }
        }

        void SaveBounds()
        {
            if (should_save)
                main_form.Settings.Save();
            should_save = false;
        }

        private void ResultForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveBounds();
            main_form.Settings.PropertyChanged -= settings_PropertyChanged;
        }

        private void webBrowser_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void contextMenuRuntimeHistory_Opening(object sender, CancelEventArgs e)
        {
            contextMenuRuntimeHistory.Items.Clear();
            foreach (string text in main_form.RuntimeHistory)
            {
                ToolStripMenuItem menu_item = new ToolStripMenuItem(text);
                menu_item.Click += new EventHandler(menu_item_Click);
                contextMenuRuntimeHistory.Items.Add(menu_item);
            }
        }

        void menu_item_Click(object sender, EventArgs e)
        {
            main_form.Navigate((sender as ToolStripMenuItem).Text);
        }

        private void contextMenuRuntimeHistory_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            contextMenuRuntimeHistory.Items.Clear();
        }
    }
}
